package cn.zooz.admin.controller;

import cn.zooz.common.result.Result;
import cn.zooz.entity.SysUser;
import cn.zooz.service.o.dto.UserDto;
import cn.zooz.service.o.vo.SysUserListVo;
import cn.zooz.service.sys.SysLoginService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by Bradish7Y on 2017/9/20.
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Resource
    private SysLoginService sysLoginService;

    /**
     * 翻页查询所有记录
     *
     * @param page
     * @return
     */
    @GetMapping(value = "/list", produces = "application/json")
    @ResponseBody
    public Result<SysUserListVo> list(Page<SysUser> page) {
        Result<SysUserListVo> result = Result.newResult();

        SysUserListVo sysUserListVo = sysLoginService.listAllSysUser(page);

        result.setResult(sysUserListVo).setCode(1000).setMessage("success").setSuccess(true);

        return result;
    }

    /**
     * 删除
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/del", produces = "application/json")
    @ResponseBody
    public Boolean del(Long userId) {
        return sysLoginService.delete(userId);
    }

    @GetMapping(value = "/update", produces = "application/json")
    @ResponseBody
    public Boolean update(UserDto userDto) {
        SysUser user = new SysUser();
        //复制bean
        BeanUtils.copyProperties(userDto, user);
        return sysLoginService.update(user);
    }
}
